package com.buka.communicate.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.buka.communicate.event.EventBuss;
import com.buka.communicate.fragment.ChatRoomListFragment;
import com.buka.communicate.fragment.MineFragment;
import com.buka.communicate.fragment.RankListFragment;
import com.buka.communicate.utils.SpringUtils;
import com.buka.communicate.utils.Tools;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.buka.communicate.R;
import com.buka.communicate.fragment.HomeFragment;
import com.buka.communicate.test.FourFragment;
import com.buka.communicate.test.ThreeFragment;
import com.buka.communicate.test.TwoFragment;

import net.neiquan.applibrary.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.AppUtils;

import butterknife.InjectView;

/**
 * 作者 ： dyj
 * 时间 ： 2016/12/29.
 */

public class MainActivity extends BaseActivity {

    @InjectView(R.id.tab_home)
    LinearLayout home_lay;
    @InjectView(R.id.tab_talent)
    LinearLayout sort_lay;
    @InjectView(R.id.tab_how)
    LinearLayout shop_lay;
    @InjectView(R.id.tab_me)
    LinearLayout mine_lay;
    @InjectView(R.id.mShopCount)
    TextView mShopCount;
    @InjectView(R.id.mDotRed)
    TextView mDotRed;
    private int preId[] = {R.mipmap.icon_sy2, R.mipmap.icon_gwc2_71, R.mipmap.icon_dd2, R.mipmap.icon_wd2};
    private int norId[] = {R.mipmap.icon_sy1, R.mipmap.icon_gwc, R.mipmap.icon_dd1, R.mipmap.icon_wd1};
    private ViewGroup Ly[];
    private FragmentTransaction fragmentTransaction;
    private Fragment fragments[] = {new HomeFragment(), new ChatRoomListFragment(), new RankListFragment(), new MineFragment()};

    @Override
    public int getRootViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void setViews() {
//        EventBus.getDefault().register(this);
        Ly = new ViewGroup[]{home_lay, sort_lay, shop_lay, mine_lay};
        setDefaultFragment(true, 0);
        AppLog.e("======getPackageName======" + AppUtils.getPackageName(this) +
                "======getSign======" + AppUtils.getSign(this) +
                "======getCertificateSHA1Fingerprint======" + AppUtils.getCertificateSHA1Fingerprint(this));
    }

//    /**
//     * 普通事件
//     *
//     * @param event
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
//    public void onDataSynEvent(EventBuss event) {
//        if (event.getState() == EventBuss.REFRESH_SHOP) {
//            int count = 0;
//            List<HomeList> goods = SpUtil.getDataList(this, "goods", HomeList.class);
//            if (goods != null && goods.size() > 0) {
//                for (int i = 0; i < goods.size(); i++) {
//                    HomeList dataList = (HomeList) goods.get(i);
//                    count += dataList.getCount();
//                }
//            }
//            mShopCount.setText("" + count);
//            mShopCount.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
//        }else if(event.getState() == EventBuss.RED_DOT_SHOW){
//            mDotRed.setVisibility(View.VISIBLE);
//        }else if(event.getState() == EventBuss.RED_DOT_HIDE){
//            mDotRed.setVisibility(View.GONE);
//        }
//    }

//    /**
//     * 粘性事件
//     * @param event
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100, sticky = true)
//    public void onDataSynEvent_(EventBuss event) {
//        if (event.getState() == EventBuss.ADD_SHOP) {
//            Log.e("info", "============ADD_SHOP222==============");
//            mShopCount.setVisibility(View.VISIBLE);
//            mShopCount.setText(""+count++);
//        } else if (event.getState() == EventBuss.DEL_SHOP) {
//            Log.e("info", "============DEL_SHOP222==============");
//            mShopCount.setText(""+count--);
//            if(count<=0){
//                mShopCount.setVisibility(View.GONE);
//            }
//        }
//    }

    @Override
    public void initData() {

    }

    private int position = 0;

    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.tab_home:
                position = 0;
//                EventBuss event1 = new EventBuss(EventBuss.HOME_REFRESH);
//                EventBus.getDefault().post(event1);
                break;
            case R.id.tab_talent:
                position = 1;
                EventBus.getDefault().post(new EventBuss(EventBuss.CHAT_ROOM_TOP));
                break;
            case R.id.tab_how:
                position = 2;
                break;
            case R.id.tab_me:
                position = 3;
                break;
            default:
                break;
        }
        setTabSelect(position);
        setDefaultFragment(false, position);
        SpringUtils.springAnim(Ly[position]);
    }

    private void setTabSelect(int position) {
        for (int i = 0; i < Ly.length; i++) {
            if (position == i) {
                if (i == 0) {
                    ((ImageView) ((FrameLayout) Ly[i].getChildAt(0)).getChildAt(0)).setImageResource(preId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.app_color));
                } else if (i == 1) {
                    ((ImageView) ((FrameLayout) Ly[i].getChildAt(0)).getChildAt(0)).setImageResource(preId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.app_color));
                } else {
                    ((ImageView) Ly[i].getChildAt(0)).setImageResource(preId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.app_color));
                }
            } else {
                if (i == 0) {
                    ((ImageView) ((FrameLayout) Ly[i].getChildAt(0)).getChildAt(0)).setImageResource(norId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.text_gray));
                } else if (i == 1) {
                    ((ImageView) ((FrameLayout) Ly[i].getChildAt(0)).getChildAt(0)).setImageResource(norId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.text_gray));
                } else {
                    ((ImageView) Ly[i].getChildAt(0)).setImageResource(norId[i]);
                    ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.text_gray));
                }
            }
        }
    }

    private void setDefaultFragment(boolean first, int index) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (first) {
            fragmentTransaction.add(R.id.framLayoutId, fragments[0]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[1]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[2]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[3]);
        }
        fragmentTransaction.hide(fragments[0]);
        fragmentTransaction.hide(fragments[1]);
        fragmentTransaction.hide(fragments[2]);
        fragmentTransaction.hide(fragments[3]);
        fragmentTransaction.show(fragments[index]);
        fragmentTransaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private long exitTime = 0;

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
